package model.controller.RacesController;


import model.race.Race;

import java.sql.*;

public class RacesDataAccesObject {
    Statement stmt = null;
    Connection conn = null;
    ResultSet r = null;
    PreparedStatement pstmt = null;

    public RacesDataAccesObject() {

    }

    public void getRaces() {
        Race race = null;
        
        try {
            connectToDatabase();
            
            stmt = conn.createStatement();
            stmt.execute("SELECT * FROM RACES");
            r = stmt.getResultSet();
            while(r.next()) {
                race = new Race((Integer) r.getObject("id"),r.getObject("date").toString(),
                        (Integer) r.getObject("numberOfParticipants"));
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            disconnect(stmt, conn);
        }
    }

    private void connectToDatabase() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Races", "root", "11111");
    }

    private void disconnect(Statement stmt, Connection conn) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
